title: “Progression Heatmap Data Preprocessing & Plotting”
author: “Venus Lau; Yen-Hsiang (Brian) Lee, Lauren Tindale”
date: “19/02/2020”
updated: “23/02/2020”
output:
html_document:
keep_md: TRUE

#Preprocessing: formatting table for heatmap

#Plotting

library(ggplot2)
library(viridis)
## Loading required package: viridisLite
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
data <- read.csv("data/COVID-19_Singapore_Heatmap_long_26-02-2020.csv")
data$date <- factor(data$date, levels=unique(data$date))
data$case <- factor(data$case, levels=unique(data$case))

data$status_word=ifelse(data$status == 0,"Unexposed",
                         ifelse(data$status == 1,"Exposed",
                                ifelse(data$status == 2,"Symptomatic",
                                       ifelse(data$status == 3,"Hospitalized","Discharged"))))

#write.csv(data, "data/COVID-19_Singapore_Heatmap_plot.csv")

p1 <- ggplot(
  data, 
  # aes(x = date, y = case, fill = status_word,
  aes(x = date, y = case, fill = status,
      text = paste("Case: ", case_detailed,
                   "<br>Date: ", date,
                   "<br>Status: ", status_word,
                   "<br>Cluster: ", cluster,
                   "<br>Citizenship: ", citizenship))) +
  geom_tile() +
  xlab(label = "Date") +
  ylab(label = "Cases") +
  ggtitle("COVID-19 Progression Amongst Singapore Cases") +
  labs(fill = "Status") + #tile fill legend label
  theme(plot.title = element_text(hjust = 0.5)) + #centre main title
  theme(axis.text.x = element_text(angle = 60, hjust = 0.6, size = 8),
        axis.ticks.x = element_blank(), #remove x axis ticks
        axis.ticks.y = element_blank()) + #remove y axis ticks
  # scale_fill_viridis_d(direction = -1) +
  scale_fill_viridis_c(direction = 1) +
  theme(panel.background = element_rect(fill = "white"))

ggplotly(p1,tooltip = 'text')
## Warning: 'heatmap' objects don't have these attributes: 'showlegend'
## Valid attributes include:
## 'type', 'visible', 'opacity', 'name', 'uid', 'ids', 'customdata', 'meta', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'z', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'transpose', 'xtype', 'ytype', 'zsmooth', 'connectgaps', 'xgap', 'ygap', 'zhoverformat', 'hovertemplate', 'zauto', 'zmin', 'zmax', 'zmid', 'colorscale', 'autocolorscale', 'reversescale', 'showscale', 'colorbar', 'coloraxis', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'zsrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
p_static=ggplot(
  data, 
  # aes(x = date, y = case, fill = status_word,
  aes(x = date, y = case, fill = status_word,
      text = paste("Case: ", case_detailed,
                   "<br>Date: ", date,
                   "<br>Status: ", status_word,
                   "<br>Cluster: ", cluster,
                   "<br>Citizenship: ", citizenship))) +
  geom_tile() +
  xlab(label = "Date") +
  ylab(label = "Cases") +
  ggtitle("COVID-19 Progression Amongst Singapore Cases") +
  labs(fill = "Status") + #tile fill legend label
  theme(plot.title = element_text(hjust = 0.5)) + #centre main title
  theme(axis.text.x = element_text(angle = 60, hjust = 0.6, size = 8),
        axis.ticks.x = element_blank(), #remove x axis ticks
        axis.ticks.y = element_blank()) + #remove y axis ticks
  # scale_fill_viridis_d(direction = -1) +
  scale_fill_viridis_d(direction = -1,breaks=c("Unexposed","Exposed","Symptomatic","Hospitalized","Discharged")) +
  theme(panel.background = element_rect(fill = "white"),
        axis.text.y = element_text(size=6),
        axis.text.x = element_text(hjust=1))

p_static

#ggsave("final_figures/Fig1c_heatmap_singapore.pdf",plot=p_static, device="pdf",width = 12,height = 8,units="in")